<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------

namespace app\user\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use app\user\model\UserModel;


/**
 * Class AdminIndexController
 * @package app\user\controller
 *
 * @adminMenuRoot(
 *     'name'   =>'用户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 10,
 *     'icon'   =>'group',
 *     'remark' =>'用户管理'
 * )
 *
 * @adminMenuRoot(
 *     'name'   =>'用户组',
 *     'action' =>'default1',
 *     'parent' =>'user/AdminIndex/default',
 *     'display'=> true,
 *     'order'  => 10000,
 *     'icon'   =>'',
 *     'remark' =>'用户组'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    /**
     * 后台本站用户列表
     * @adminMenu(
     *     'name'   => '本站用户',
     *     'parent' => 'default1',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $content = hook_one('user_admin_index_view');

        if (!empty($content)) {
            return $content;
        }

        $list = Db::name('user')
        ->where(function (Query $query) {
            $data = $this->request->param();
            if (!empty($data['uid'])) {
                $query->where('id', intval($data['uid']));
            }

            if (!empty($data['keyword'])) {
                $keyword = $data['keyword'];
                $query->where('user_login|user_nickname|user_email|mobile', 'like', "%$keyword%");
            }

        })
        ->order("create_time DESC")
        ->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }
    public function edit(){

        $id = $this->request->param('id', 0, 'intval');
        $usermodel = new UserModel();
        $post  = $usermodel->where('id', $id)->find();
        $this->assign('post', $post);
        return $this->fetch();
    }
    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post   = $data['post'];
            $where['id'] = $post['id'];
            unset($post['id']);
            $Model = new UserModel();
            $rel = $Model->editUserInfo($where, $post);
            $this->success('保存成功!');

        }
    }
    public function balance(){

        $id = $this->request->param('id', 0, 'intval');
        $usermodel = new UserModel();
        $post  = $usermodel->where('id', $id)->find();
        $this->assign('post', $post);
        return $this->fetch();
    }
    public function balancePost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $Model =   new UserModel();
            $post   = $data['post'];
            $where['id'] = $post['id'];
            $info  = $Model->where($where)->find();
            unset($post['id']);
            $update['balance'] =  $info['balance'] + $post['balance'];

            $rel = $Model->editUserInfo($where, $update);

            if($rel){
                $insert['user_id'] = $info['id'];
                $insert['change'] = $post['balance'];
                $insert['create_time'] = time();
                $insert['balance'] = $update['balance'];
                Db::name('user_balance_log')->insert($insert);
                $this->success('充值成功!');
            }
            $this->error('充值失败');
        }
    }

    public function order_list()
    {

        $list = Db::name('order')
        ->where(function (Query $query) {
            $data = $this->request->param();
            if (!empty($data['uid'])) {
                $query->where('user_id', intval($data['uid']));
            }

            if (!empty($data['keyword'])) {
                $keyword = $data['keyword'];
                $query->where('user_name|good_name|consignee', 'like', "%$keyword%");
            }

        })
        ->order("create_time DESC")
        ->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }
    // 查看订单
    public function order_edit(){

        $id = $this->request->param('id', 0, 'intval');

        $post  =Db::name("order")->where('id', $id)->find();
        $this->assign('post', $post);
        return $this->fetch();
    }
    // 充值列表
    public function balance_list(){

        $list = Db::name('user_balance_log')->alias('b')->join('user u','b.user_id=u.id')
        ->where(function (Query $query) {
            $data = $this->request->param();
            if (!empty($data['uid'])) {
                $query->where('user_id', intval($data['uid']));
            }
        })
        ->where('type',1)
        ->field('b.id,b.user_id,b.create_time,b.change,b.state,b.img,u.user_login')
        ->order("create_time DESC")
        ->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();

    }

    public function balance_edit(){

        $id = $this->request->param('id', 0, 'intval');
        $post  =  Db::name('user_balance_log')->alias('b')->join('user u','b.user_id=u.id')->where('b.id', $id)
        ->field('b.id,b.user_id,b.create_time,b.change,b.state,b.img,u.user_login')->find();
        $this->assign('post', $post);
        return $this->fetch();
    }
    public function balanceePost()
    {

     if ($this->request->isPost()) {
        $data = $this->request->param();

        $post   = $data['post'];
        $where['id'] = $post['id'];
        unset($post['id']);
        $post['update_time'] = time();

        $logrel=  Db::name('user_balance_log')->where($where)->update($post);
        if($logrel){
            if($post['state']==1){
                $logInfo =  Db::name('user_balance_log')->where($where)->find();
                $uwhere['id'] = $logInfo['user_id'];
                $Model =   new UserModel();
                $info  = $Model->where($uwhere)->find();

                $update['balance'] =  $info['balance'] + $logInfo['change'];
                $rel = $Model->editUserInfo($uwhere, $update);
                $this->success('审核成功，审核结果通过!');
            }
            $this->success('审核成功，审核结果不通过!');

        }
        $this->error('审核失败');
    }

}
public function bankconfig(){
    $id = 1;
    $post  =Db::name("bankconfig")->where('id', $id)->find();
    $this->assign('post', $post);
    return $this->fetch();
}
  public function bankconfigPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $post   = $data['post'];
            $where['id'] = 1;
            $rel = Db::name("bankconfig")->where($where)->update($post);
            $this->success('保存成功!');

        }
    }
public function score_list(){

        $list = Db::name('user_score_log')->alias('b')->join('user u','b.user_id=u.id')
        ->where(function (Query $query) {
            $data = $this->request->param();
            if (!empty($data['uid'])) {
                $query->where('user_id', intval($data['uid']));
            }
        })
        ->where('status',3)
        ->field('b.id,b.user_id,b.create_time,b.score,b.state,b.status,b.state,u.user_login')
        ->order("create_time DESC")
        ->paginate(10);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();

    }
      public function score_edit(){

        $id = $this->request->param('id', 0, 'intval');
        $post  =  Db::name('user_score_log')->alias('b')->join('user u','b.user_id=u.id')->where('b.id', $id)
        ->field('b.id,b.user_id,b.create_time,b.score,b.state,b.update_time,b.state,u.user_login,b.bank_name,b.bank_num,b.card_username,b.usdt_addres')->find();
        $this->assign('post', $post);
        return $this->fetch();
    }
    public function scorePost()
    {

     if ($this->request->isPost()) {
        $data = $this->request->param();

        $post   = $data['post'];
        $where['id'] = $post['id'];
        unset($post['id']);
        $post['update_time'] = time();

        $logrel=  Db::name('user_score_log')->where($where)->update($post);
        if($logrel){
            if($post['state']==1){
                $logInfo =  Db::name('user_score_log')->where($where)->find();
                $uwhere['id'] = $logInfo['user_id'];
                $Model =   new UserModel();
                $info  = $Model->where($uwhere)->find();

                $update['score'] =  $info['score'] - $logInfo['score'];
                $rel = $Model->editUserInfo($uwhere, $update);
                $this->success('审核成功，审核结果通过!');
            }
            $this->success('审核成功，审核结果不通过!');

        }
        $this->error('审核失败');
    }

}

    /**
     * 本站用户拉黑
     * @adminMenu(
     *     'name'   => '本站用户拉黑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户拉黑',
     *     'param'  => ''
     * )
     */
    public function ban()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            $result = Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 0);
            if ($result) {
                $this->success("会员拉黑成功！", "adminIndex/index");
            } else {
                $this->error('会员拉黑失败,会员不存在,或者是管理员！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 本站用户启用
     * @adminMenu(
     *     'name'   => '本站用户启用',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '本站用户启用',
     *     'param'  => ''
     * )
     */
    public function cancelBan()
    {
        $id = input('param.id', 0, 'intval');
        if ($id) {
            Db::name("user")->where(["id" => $id, "user_type" => 2])->setField('user_status', 1);
            $this->success("会员启用成功！", '');
        } else {
            $this->error('数据传入失败！');
        }
    }
}
